package com.digital.smartgrow.controller.process;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.dto.process.UpProcessItemPageDTO;
import com.digital.smartgrow.entity.expert.Expert;
import com.digital.smartgrow.entity.process.UpProcess;
import com.digital.smartgrow.entity.process.UpProcessItem;
import com.digital.smartgrow.service.process.UpProcessItemService;
import com.digital.smartgrow.service.process.UpProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 审批流节点
 前端控制器
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-12
 */
@Api(tags = "审批流节点 ")
@RestController
@RequestMapping("${context.prefix}/syc/upProcessItem")
public class UpProcessItemController {

    @Autowired
    private UpProcessItemService upProcessItemService;
    @Autowired
    private UpProcessService upProcessService;

    @ApiOperation(value = "审批流节点 -分页查询")
    @PostMapping("/list")
    public Object getListByPage(@RequestBody UpProcessItemPageDTO upProcessItemPageDTO){
        return upProcessItemService.getListByPage(upProcessItemPageDTO);
    }

    @ApiOperation(value = "审批流节点 -详情")
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable Integer id){
        if (id == null) return Result.fail("id不能为空");
        return upProcessItemService.getInfo(id);
    }

    @ApiOperation(value = "审批流节点 -删除")
    @DeleteMapping("/{id}")
    public Object removeById(@PathVariable Integer id){
      if (id == null) return Result.fail("id不能为空");
        UpProcessItem upProcessItem = upProcessItemService.getById(id);
        UpProcess upProcess = upProcessService.getById(upProcessItem.getSuperCode());
        if(upProcess==null){
            return Result.fail("主流程不存在");
        }
        String  str=upProcess.getSubCode();
        String removeStr="["+upProcessItem.getId()+"]";
        StringUtils.remove(str, removeStr);
        upProcess.setSubCode(str);
        upProcessService.updateById(upProcess);

      return upProcessItemService.removeById(id);
    }
    @ApiOperation(value = "审批流节点 -新增")
    @PostMapping()
    public Object add( @RequestBody UpProcessItem upProcessItem){
        if (upProcessItem.getSuperCode() == null) return Result.fail("所属流程id不能为空");
        UpProcess upProcess = upProcessService.getById(upProcessItem.getSuperCode());
        if(upProcess==null){
            return Result.fail("主流程不存在");
        }
        upProcess.setSubCode(upProcess.getSubCode()+"["+upProcessItem.getId()+"]");
        upProcessService.updateById(upProcess);
        return upProcessItemService.save(upProcessItem);
    }

    @ApiOperation(value = "审批流节点 -批量新增")
    @PostMapping("/save")
    public Object add( @RequestBody List<UpProcessItem> upProcessItems){
        int size = upProcessItems.size();
        UpProcess upProcess =new UpProcess();
        if(size>0){
            if (upProcessItems.get(0).getSuperCode() == null) {
                return Result.fail("所属流程id不能为空");
            }else {
                upProcess=upProcessService.getById(upProcessItems.get(0).getSuperCode());
            }
        }else {
            return Result.fail("节点不能为空");
        }
        //删除原数据
        QueryWrapper<UpProcessItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UpProcessItem::getSuperCode, upProcessItems.get(0).getSuperCode());
        upProcessItemService.remove(queryWrapper);


        String subCode="";
        for(UpProcessItem upProcessItem:upProcessItems){
            upProcessItemService.save(upProcessItem);
            subCode="["+upProcessItem.getId()+"]";
        }

        upProcess.setCount(size);
        upProcess.setSubCode(subCode);
        upProcessService.updateById(upProcess);

        return true;
    }

    @ApiOperation(value = "审批流节点 -编辑")
    @PutMapping()
    public Object edit(@RequestBody UpProcessItem upProcessItem){
    if (upProcessItem==null||upProcessItem.getId() == null)
       return Result.fail("id不能为空");
    if(upProcessItem.getSuperCode()!=null){
        return Result.fail("所属不可修改");
    }
       return upProcessItemService.updateById(upProcessItem);
    }
}
